package com.lucas.rpc.framework.transport.tomcat;

import com.lucas.rpc.framework.remote.RpcRequest;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author: LiuCheng
 * @description:
 * @date: 2023-11-28 08:54
 **/
public class TomcatClient {

    public String send(String hostname, Integer port, RpcRequest rpcRequest){

        try {

            URL url = new URL("http",hostname,port,"/");

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            httpURLConnection.setRequestMethod("POST");

            httpURLConnection.setDoOutput(true);

            OutputStream outputStream = httpURLConnection.getOutputStream();

            ObjectOutputStream oos = new ObjectOutputStream(outputStream);

            oos.writeObject(rpcRequest);

            oos.flush();

            oos.close();

            InputStream inputStream = httpURLConnection.getInputStream();

            String result = IOUtils.toString(inputStream);

            return result;


        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

}
